package v9.assist.script


import sample.ui.AppDispatchers
import sample.ui.Properties
import sample.ui.ConsoleLayout
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.script.Block
import kotlinx.coroutines.script.C
import kotlinx.coroutines.script.Controller
import kotlinx.coroutines.script.Controller.Companion.processExitGame
import kotlinx.coroutines.script.accAutoAtk
import kotlinx.coroutines.script.tes.DiskLogUtils
import kotlinx.coroutines.script.tes.appScopeLaunch
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.pt
import kotlinx.coroutines.script.tes.random
import kotlinx.coroutines.script.tes.readByDisk
import kotlinx.coroutines.script.tes.shake
import kotlinx.coroutines.script.tes.toast
import v9.assist.access.accClick
import v9.assist.access.accPathfinding
import v9.assist.access.btnApplyAccept
import v9.assist.access.btnApplyMenu
import v9.assist.access.btnApplyPopMenu
import v9.assist.access.btnApplyRefuse
import v9.assist.access.btnCloseable
import v9.assist.access.btnEditFriendClose
import v9.assist.access.btnInteract
import v9.assist.access.btnInviteBlackClose
import v9.assist.access.btnInviteCancel
import v9.assist.access.btnInviteConfirm
import v9.assist.access.btnPopApplyClose
import v9.assist.access.btnPopTxtCancel
import v9.assist.access.btnPopTxtConfirm
import v9.assist.access.btnRandomBuff
import v9.assist.access.btnReviveByOtherCancel
import v9.assist.access.btnReviveInCity
import v9.assist.access.btnReviveInHere
import v9.assist.access.btnSceneHideState
import v9.assist.access.btnSceneInviteGroupAllClose
import v9.assist.access.btnScreenCenter
import v9.assist.access.btnSelectRole
import v9.assist.access.btnSkipAnim
import v9.assist.access.releaseLastGestures
import v9.assist.analyst.MODE_PATHFINDING_P1
import v9.assist.analyst.MODE_PATHFINDING_P2
import v9.assist.analyst.PAGE_APPLY
import v9.assist.analyst.PAGE_APPLY_EMPTY
import v9.assist.analyst.PAGE_BASE_MENU
import v9.assist.analyst.PAGE_BE_INVITED
import v9.assist.analyst.PAGE_BE_INVITED_BLACK_GROUP
import v9.assist.analyst.PAGE_BOSS_DIRECT_OR_BUFF
import v9.assist.analyst.PAGE_CLOSEABLE
import v9.assist.analyst.PAGE_EDIT_FRIEND_NAME
import v9.assist.analyst.PAGE_INITIAL
import v9.assist.analyst.PAGE_MAIN
import v9.assist.analyst.PAGE_POP_TEXT
import v9.assist.analyst.PAGE_REVIVE
import v9.assist.analyst.PAGE_REVIVE_BY_OTHER
import v9.assist.analyst.PAGE_ROOT_ENTER
import v9.assist.analyst.PAGE_ROOT_ENTER_TIP
import v9.assist.analyst.PAGE_SCENE_COMPLETED
import v9.assist.analyst.PAGE_SCENE_ENTER
import v9.assist.analyst.PAGE_SCENE_GROUP_INVITE_PLAYER
import v9.assist.analyst.PAGE_SELECT_ROLE
import v9.assist.analyst.PAGE_SKIP
import v9.assist.analyst.PAGE_TALK_BLACKSMITH
import v9.assist.analyst.anaClearSceneSettle
import v9.assist.analyst.anaLastHasBackpack100Ts
import v9.assist.analyst.anaLastHasEnemyNameTs
import v9.assist.analyst.anaLastHasInteractTs
import v9.assist.analyst.anaLastSMapJTFeature1Ts
import v9.assist.analyst.anaLastSMapJTFeature2Ts
import v9.assist.analyst.anaLastSMapJTFeature3Ts
import v9.assist.analyst.anaLastSMapJTFeature4Ts
import v9.assist.analyst.anaLastSMapJTFeature5Ts
import v9.assist.analyst.anaLastSMapJTFeature6Ts
import v9.assist.analyst.anaLastSMapJTFeature7Ts
import v9.assist.analyst.anaLastSMapJTFeature8Ts
import v9.assist.analyst.anaLastSMapJTFeature9Ts
import v9.assist.analyst.anaLastSceneSettleTs
import v9.assist.analyst.anaPathfindingModeJT
import v9.assist.analyst.anaUpdateExcludeEnemyName
import v9.assist.analyst.analystInitialized
import v9.assist.analyst.angleByRoad
import v9.assist.analyst.blockBlacksmith
import v9.assist.analyst.blockBlueDoor
import v9.assist.analyst.blockEliteEnemyBoss
import v9.assist.analyst.bsAcceptApply
import v9.assist.analyst.bsAcceptInvite
import v9.assist.analyst.bsBossBeside
import v9.assist.analyst.bsEnemy
import v9.assist.analyst.bsHasApplyDot
import v9.assist.analyst.bsHasEnemy
import v9.assist.analyst.bsHasInteract
import v9.assist.analyst.bsHasReviveRightBtn
import v9.assist.analyst.bsHasTransmitting
import v9.assist.analyst.bsPage
import v9.assist.analyst.bsPlayerHpLosing
import v9.assist.analyst.bsSceneSettle
import v9.assist.analyst.bsSceneSettleBlacksmith
import v9.assist.analyst.counterActionStopped
import v9.assist.analyst.fsInfo
import v9.assist.analyst.lastEliteEnemyBossTs
import v9.assist.analyst.lastSMapTargetToNpcTs
import v9.assist.analyst.setupAnalystScene
import kotlin.math.max

private var scriptInError = false
private var n: Long? = null // 检查CDK用的时间戳标记
private var job: Job? = null

private var sLastFeature1Ts = 0L
private var sLastFeature2Ts = 0L
private var sLastFeature3Ts = 0L // 直通
private var sLastFeature4Ts = 0L // 副本外
private var sLastFeature5Ts = 0L // 直通C2
private var sLastFeature6Ts = 0L //
private var sLastFeature7Ts = 0L //
private var sLastFeature8Ts = 0L //
private var sLastFeature9Ts = 0L //


fun startSceneJT() {
    job?.cancel()
    job = appScopeLaunch(AppDispatchers.Script) {
        scriptInError = false
        xCounterNetRetry = Properties.netRetryCount
        xLastAtvTs = System.currentTimeMillis()
        xLastSceneEnterTs = System.currentTimeMillis() // 现在只记录副本开始时间
        setupAnalystScene(C.S_JT)
        anaUpdateExcludeEnemyName("")
        n = System.currentTimeMillis()
        delay(200) 
        while(Controller.isRunning && !scriptInError) {
            checkScriptError()
            if (!analystInitialized) {
                printLog { "Script invoking not ready" }
                ConsoleLayout.outputText("等待首帧截屏.连续出现重启脚本")
                toast("等待首帧截屏.连续出现重启脚本")
                delay(1000) // 刚开始没准备好
            } else {
                val timestamp = System.currentTimeMillis()
                processFsJT()
                val using = System.currentTimeMillis() - timestamp
                if (using < 10) delay(if (bsPage == PAGE_INITIAL) 800L.shake(50) else 200L.shake(5))
            }
        }
    }
}

fun stopSceneJT() {
    xSceneHideState = false
    xBossDirectOrBuff = false
    xLastAtvTs = 0 // 不能少
    job?.cancel()
    anaClearSceneSettle()
}

suspend fun processFsJT() {
    fsInfo.also { if (bsPage != SCRIPT_MAIN) printLog { "processFs $anaPathfindingModeJT $anaPathfindingModeJT $anaPathfindingModeJT $anaPathfindingModeJT $it" } }

    if (bsPage == PAGE_BE_INVITED) {
        val widget = if (bsAcceptInvite) btnInviteConfirm else btnInviteCancel
        accClick(widget, afterDelay = 500)
        random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        }
    } else if (bsPage == PAGE_REVIVE) {
        if (bsHasReviveRightBtn) {
            accClick(btnReviveInHere, afterDelay = 333)
        } else {
            accClick(btnReviveInCity, afterDelay = 333)
            if (xBossDirectOrBuff) xLastBossDirectOrBuffTs = System.currentTimeMillis()

            if (anaPathfindingModeJT == MODE_PATHFINDING_P2) {
                // 起点复活通过传送门快速回到boss
                delay(1000L) // 不然 移动卡掉了.反而原地点了弹窗.走了全程路径
                listOf(140, 135, 140, 135, 140, 140).forEachIndexed { index, i ->
                    printLog { "scriptJT 节点.复活直接去boss $index" }
                    accPathfinding(i, if (index > 0) btnInteract else null)
                }
            } else if (anaPathfindingModeJT == MODE_PATHFINDING_P1) {
                delay(1000L)
                listOf(310, 315, 310, 315, 310, 315, 310, 315, ).forEachIndexed { index, i ->
                    printLog { "scriptJT 节点.教堂路线是起点 A $index" }
                    accPathfinding(i, if (index >= 4) btnInteract else null)
                }
            }
        }
        xLastAtvTs = System.currentTimeMillis()
        xLastReviveTs = System.currentTimeMillis()
    } else if (bsPage == PAGE_SCENE_ENTER) {
        accSceneEnter()
        anaPathfindingModeJT = MODE_PATHFINDING_P1
    } else if (bsPage == PAGE_BASE_MENU) {
        accClick(btnScreenCenter, afterDelay = 2000)
    } else if (bsPage == PAGE_SKIP) {
        delay(500)
        accClick(btnSkipAnim, afterDelay = 2500)
        listOf(316, 316, 316, 316).forEachIndexed { index, i ->
            printLog { "script 画面跳过 $index" }
            accPathfinding(i)
        }
        releaseLastGestures()
        xLastAtvTs = System.currentTimeMillis()
    } else if (bsPage == PAGE_POP_TEXT) {
        accClick(btnPopTxtCancel, afterDelay = 2400)
        listOf(316, 316, 316, 316, 316, 316, 316).forEachIndexed { index, i ->
            printLog { "script 防止卡地图移动 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        accClick(btnInteract, afterDelay = 1500)
        accClick(btnPopTxtConfirm, afterDelay = 2000) // 如果是在外面的入口卡住
        xLastAtvTs = System.currentTimeMillis() // 延长时间
    } else if (bsPage == PAGE_BE_INVITED_BLACK_GROUP) {
        delay(500)
        accClick(btnInviteBlackClose, afterDelay = 1500)
    } else if (bsPage == PAGE_EDIT_FRIEND_NAME) {
        delay(500)
        accClick(btnEditFriendClose, afterDelay = 1500)
        random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        }
    } else if (bsPage == PAGE_SCENE_GROUP_INVITE_PLAYER) {
        delay(500)
        accClick(btnSceneInviteGroupAllClose, afterDelay = 1500)
        random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        }
    } else if (bsPage == PAGE_TALK_BLACKSMITH || bsPage == PAGE_SCENE_COMPLETED
        || bsSceneSettleBlacksmith || bsSceneSettle
    ) {
        if (bsEnemy == "Y" || bsEnemy == "B" || bsEnemy == "P" || blockEliteEnemyBoss != null
            && System.currentTimeMillis() - sLastFeature34Ts > 1000) {
            anaClearSceneSettle()
            sLastFeature34Ts = System.currentTimeMillis()
        }
        else {
            xLastAtvTs = System.currentTimeMillis()
            sceneHandlingSettle(
                blacksmithBy = { blacksmithByBlueDoor(blockBlueDoor) ?: blacksmithByBlacksmith(blockBlacksmith) },
                exitBy = { exitByBlueDoor(blockBlueDoor) ?: exitByBlacksmith(blockBlacksmith) }
            )
        }
    } else if (bsPage == PAGE_APPLY_EMPTY) {
        accClick(btnPopApplyClose, afterDelay = 1000)
    } else if (bsPage == PAGE_APPLY) {
        accClick(if (bsAcceptApply) btnApplyAccept else btnApplyRefuse, afterDelay = 1000)
    } else if (bsPage == PAGE_SELECT_ROLE) {
        accClick(btnSelectRole, afterDelay = 1500)
    } else if (bsPage == PAGE_CLOSEABLE) {
        delay(500)
        accClick(btnCloseable, afterDelay = 3000)
        xLastCloseableTs = System.currentTimeMillis()
    } else if (bsPage == PAGE_BOSS_DIRECT_OR_BUFF) {
        delay(500)
        accClick(btnRandomBuff, afterDelay = 2000)
        xLastBossDirectOrBuffTs = System.currentTimeMillis()
        xBossDirectOrBuff = true
    } else if (bsPage == PAGE_REVIVE_BY_OTHER) {
        accClick(btnReviveByOtherCancel, afterDelay = 1500)
    } else if (bsPage == PAGE_ROOT_ENTER_TIP || bsPage == PAGE_ROOT_ENTER) {
        tryEnterByRelaunch()
    } else if (bsPage == PAGE_MAIN) { // 主页操作
        if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) {
            accAutoAtk(angleByRoad)
            xLastAtvTs = System.currentTimeMillis()
            if (bsHasEnemy) xLastAtkTs = System.currentTimeMillis()
        } else if (
            (System.currentTimeMillis() - xLastBossDirectOrBuffTs < 30000 || (System.currentTimeMillis() - xLastReviveTs < 30000 && xBossDirectOrBuff))
            && System.currentTimeMillis() - anaLastHasEnemyNameTs > 3000
            && blockBlueDoor != null) {
            listOf(340, 340, 340, 340, 340, 340, 340, 340, 340, 340, 340, 340, 340).forEachIndexed { index, i ->
                printLog { "scriptJT 直通直接去bossA1 $index" }
                accPathfinding(i)
            }
            listOf(205, 205, 205, 205, 205, 205).forEachIndexed { index, i ->
                printLog { "scriptJT 直通直接去bossA2 $index" }
                accPathfinding(i, if (index >= 2) btnInteract else null)
            }
            delay(1000L)
        } else if (bsHasTransmitting
            && !bsPlayerHpLosing
            && System.currentTimeMillis() - anaLastHasInteractTs < 9000) {
            // 开门流程
            xCounterInteract = 0
            delay(500)
        } else if (System.currentTimeMillis() - anaLastHasBackpack100Ts < 50000
            && (System.currentTimeMillis() - xLastAtkTs > 3000 || System.currentTimeMillis() - anaLastSceneSettleTs < 10000)
            && System.currentTimeMillis() - sLastFeature29Ts > 50000) {
            printLog { "script 满背包尝试分解" }
            tryPetBlacksmithSubmit()
            sLastFeature29Ts = System.currentTimeMillis()
        } else if (bsHasInteract && !bsPlayerHpLosing
            && System.currentTimeMillis() - xLastUnknownTs > 8000
            && System.currentTimeMillis() - xLastSceneEnterTs > 12000
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 16000  // 击杀boss后不要乱点
            && System.currentTimeMillis() - xLastBossDirectOrBuffTs > 20000
            && !bsSceneSettle && !bsSceneSettleBlacksmith
            && System.currentTimeMillis() - anaLastSceneSettleTs > 15000
            && System.currentTimeMillis() - xLastCloseableTs > 10000) {
            xCounterInteract++
            if (xCounterInteract % 6 == 0) { // 超过？秒即可。 互动进度条 6秒够了
                printLog { "新增移动。避免背包满了" }
                accPathfinding(angleByRoad)
                random.nextInt(360).also { 
            listOf(it, it, it, it).forEachIndexed { index, i -> 
                printLog { "script 随机移动 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
        }
            }

            val delay = // NPC解救互动连续点击慢一点
                if (System.currentTimeMillis() - lastSMapTargetToNpcTs < 5000) 2500L
                else 1500L
            accClick(btnInteract, afterDelay = delay)
        } else if (bsHasApplyDot && Properties.inviteAutoAccept != 0) {
            accClick(btnApplyMenu, afterDelay = 500)
            accClick(btnApplyPopMenu, afterDelay = 1000)
        } else {
            // 寻路流程
            xCounterInteract = 0
            sceneJTSpecialHandling1()
            accPathfinding(sceneAngleByRoad())
        }
    } else if (bsPage != PAGE_MAIN) {
        scriptHandlingUnknown()
    }
}


private var sLastFeature2Child1Ts = 0L //回头攻击
private var sCounterFeature2Child1 = 0
private var flagLastFeature21 = false // 返回教堂电梯的等待60秒

suspend fun sceneJTSpecialHandling1() {
    if (System.currentTimeMillis() - xLastAtkTs > 240000
        && System.currentTimeMillis() - xLastAtvTs > 240000
        && System.currentTimeMillis() - xLastSceneExitTs > 120000) {
        printLog { "scriptJT 节点.超时退出" }
        sceneExit()
        anaPathfindingModeJT = MODE_PATHFINDING_P1 // 重制
    } else if (counterActionStopped > 30 && System.currentTimeMillis() - xLastAtkTs > 3000
        && System.currentTimeMillis() - sLastFeature33Ts > 60000) {
        printLog { "script 节点.原地不动." }
        releaseLastGestures()
//        accClick(btnSmallMap, afterDelay = 2000)
        counterActionStopped = 0
        sLastFeature33Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature1Ts < 2000
        && System.currentTimeMillis() - sLastFeature1Ts > 16000) {
        printLog { "scriptJT 节点.地下起点电梯" }
        if (System.currentTimeMillis() - xLastReviveTs > 20000) { // 被boss击杀后直接走
            listOf(270, 280, 270, 270, 280, 270, 280, 270, 280, 270, 270, 80, 95).forEachIndexed { index, i ->
                printLog { "scriptJT 节点.地下起点位置调整 $index" }
                accPathfinding(i)
            }
            releaseLastGestures()
            if (flagLastFeature21) {
                flagLastFeature21 = false
                repeat(16) {
                    printLog { "scriptJT 节点.地下起点位置调整等待中B $it" }
                    delay(5000L)
                }
            } else {
                repeat(3) {
                    printLog { "scriptJT 节点.地下起点位置调整等待中A $it" }
                    delay(5000L)
                }
            }
        }
        // 这里直接往右下走
        listOf(135, 140, 135, 140, 135, 140, 135, 140, 135, 140, 135,
            140, 135, 140, 135, 140, 110, 90, 70, 70, 70).forEachIndexed { index, i ->
            printLog { "scriptJT 节点.地下起点位置调整 2段 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        releaseLastGestures()
        sLastFeature1Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature2Ts < 2000
        && System.currentTimeMillis() - sLastFeature2Ts > 1000) {
        printLog { "scriptJT 节点.仪式2 $sCounterFeature2Child1" }
        if (System.currentTimeMillis() - sLastFeature2Child1Ts < 4000) {
            val limit = if (System.currentTimeMillis() - anaLastHasEnemyNameTs < 2000) 1 else 2
            sCounterFeature2Child1++ // 回头计数
            if (sCounterFeature2Child1 > limit) {
                listOf(0, 350, 180, 215, 210, 215, 210, 215, 210, 215).forEachIndexed { index, i ->
                    if (System.currentTimeMillis() - lastEliteEnemyBossTs > 2000) {
                        printLog { "scriptJT 节点.地下仪式2 回头清理敌人A $index" }
                        if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
                        else accPathfinding(i)
                    }
                }
                listOf(235, 230, 235, 270, 230, 235).forEachIndexed { index, i ->
                    if (System.currentTimeMillis() - lastEliteEnemyBossTs > 2000) {
                        printLog { "scriptJT 节点.地下仪式2 回头清理敌人B $index" }
                        if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
                        else accPathfinding(i)
                    }
                }
                var counter = 60
                while (counter-- > 0) {
                    if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) {
                        printLog { "scriptJT 节点.地下仪式2 战斗清理" }
                        accAutoAtk(random.nextInt(360))
                    }
                    else if (counter % 5 == 0) {
                        printLog { "scriptJT 节点.地下仪式2 随机移动" }
                        accPathfinding(random.nextInt(360))
                        releaseLastGestures()
                    }
                    else if (System.currentTimeMillis() - anaLastHasEnemyNameTs < 3000){
                        printLog { "scriptJT 节点.地下仪式2 待机" }
                        delay(500)
                        releaseLastGestures()
                    }
                }
                sCounterFeature2Child1 = 0
            }
        } else sCounterFeature2Child1 = 0
        sLastFeature2Child1Ts = System.currentTimeMillis()

        sLastFeature2Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature3Ts < 2000
        && System.currentTimeMillis() - sLastFeature3Ts > 6000) {
        listOf(340, 340, 340, 340, 340, 340, 340, 340, 340, 340, 340, 340, 340).forEachIndexed { index, i ->
            printLog { "scriptJT 直通直接去bossA1 $index" }
            accPathfinding(i)
        }
        listOf(205, 205, 205, 205, 205, 205,).forEachIndexed { index, i ->
            printLog { "scriptJT 直通直接去bossA2 $index" }
            accPathfinding(i, if (index >= 2) btnInteract else null)
        }
        sLastFeature3Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature5Ts < 2000
        && System.currentTimeMillis() - sLastFeature5Ts > 6000) {
        listOf(70, 70, 70, 70, 70, 70,).forEachIndexed { index, i ->
            printLog { "scriptJT 节点.直通直接去bossB $index" }
            accPathfinding(i, if (index > 0) btnInteract else null)
        }
        delay(1000L)
        sLastFeature5Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature8Ts < 2000
        && System.currentTimeMillis() - anaLastSMapJTFeature6Ts > 15000
        && System.currentTimeMillis() - sLastFeature8Ts > 6000) {
        listOf(135, 135, 135).forEachIndexed { index, i ->
            printLog { "scriptJT 节点.直通直接去bossC $index" }
            accPathfinding(i, if (index >= 0) btnInteract else null)
        }
        delay(1000L)
        sLastFeature8Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature4Ts < 2000
        && System.currentTimeMillis() - sLastFeature4Ts > 6000) {
        listOf(330, 330, 330, 330, 330, 330, 330, 330).forEachIndexed { index, i ->
            printLog { "scriptJT 节点.副本外入口 $index" }
            accPathfinding(i, if (index > 1) btnInteract else null)
        }
        accClick(btnPopTxtConfirm, afterDelay = 2000) // 如果是在外面的入口卡住
        sLastFeature4Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature7Ts < 2000
        && System.currentTimeMillis() - sLastFeature7Ts > 6000) {
        anaPathfindingModeJT = MODE_PATHFINDING_P1
        listOf(240, 235, 240, 235, 240, 235, 240, 235, 270, 275, 270, 275, 330, 350, 330, 325).forEachIndexed { index, i ->
            printLog { "scriptJT 节点.伪P2回头 $index" }
            accPathfinding(i)
        }
        flagLastFeature21 = true
        sLastFeature7Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature6Ts < 2000
        && System.currentTimeMillis() - sLastFeature6Ts > 6000) {
        if (!xSceneHideState) {
            printLog { "script. 收起副本状态" }
            accClick(btnSceneHideState, afterDelay = 500)
            xSceneHideState = true
        }
        listOf(310, 315, 310, 315, 310, 315, 310, 315, 310, 315, 310).forEachIndexed { index, i ->
            printLog { "scriptJT 节点.教堂路线是起点 B $index" }
            accPathfinding(i, if (index >= 4) btnInteract else null)
        }
        listOf(315, 315, 315, 315, 315, 315).forEachIndexed { index, i ->
            printLog { "scriptJT 节点.教堂路线是起点 B 延续 $index" }
            if (bsHasEnemy || bsPlayerHpLosing || bsBossBeside) accAutoAtk(i)
            else accPathfinding(i)
        }
        sLastFeature6Ts = System.currentTimeMillis()
    } else if (System.currentTimeMillis() - anaLastSMapJTFeature9Ts < 2000
        && System.currentTimeMillis() - sLastFeature9Ts > 6000
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 70000
        && System.currentTimeMillis() - xLastAtkTs > 70000) {
        printLog { "analystJT 节点.Boss终点 无boss" }
        sceneExit()
        anaPathfindingModeJT = MODE_PATHFINDING_P1 // 重制
        sLastFeature9Ts = System.currentTimeMillis()
    }

}

private fun blacksmithByBlueDoor(blueDoor: Block?): Block? {
    if (blueDoor == null) return null
    return Block("铁匠", blueDoor.x - 44.pt, blueDoor.y - 40.pt, 0, 0)
}
private fun blacksmithByBlacksmith(blacksmith: Block?): Block? {
    if (blacksmith == null) return null
    return Block("铁匠fixed", blacksmith.x - 2.pt, blacksmith.y - 0.pt, 0, 0)
}
private fun exitByBlueDoor(blueDoor: Block?): Block? {
    if (blueDoor == null) return null
    return Block("小地图传送门fixed", blueDoor.x - 2.pt, blueDoor.y - 3.pt, 0, 0)
}
private fun exitByBlacksmith(blacksmith: Block?): Block? {
    if (blacksmith == null) return null
    return Block("小地图传送门By铁匠", blacksmith.x + 44.pt, blacksmith.y + 40.pt, 0, 0)
}

private suspend fun checkScriptError() {
    if (xLastAtvTs == 0L) {
        return
    }
    if (xScriptEndTs > 0 && System.currentTimeMillis() > xScriptEndTs) {
        scriptInError = true
        processExitGame("定时结束(${Properties.stopCountdown}秒)")
        DiskLogUtils.saveToDisk()
    }
    val timeout = Properties.scriptStopTimeout * 1000L   // 超过分钟没有敌人停止脚本
    xAtvLeftTime = max(0, timeout - (System.currentTimeMillis() - xLastAtvTs))
    if (System.currentTimeMillis() - xLastAtvTs > timeout) {
        scriptInError = true
        processExitGame("${C.ERROR_END}=${timeout / 1000}")
        DiskLogUtils.saveToDisk()
    }
    val ne = n
    if (ne != null && System.currentTimeMillis() - ne > timeout
        && System.currentTimeMillis() - lastEliteEnemyBossTs > timeout) {
        // 还有一种情况. 就是已经运行了x分钟了.但是没有boss 可能卡遗迹了
        scriptInError = true
        processExitGame("${C.ERROR_END}=BossLoss")
        DiskLogUtils.saveToDisk()
    }
    if (ne != null && System.currentTimeMillis() - ne > 198123L) {
        val sp = "j#s#l#t#z#m#p#e#r#a".split("#")
        var pass = true
        sp.forEach {
            if (pass && readByDisk(it).isNullOrEmpty()) pass = false
        }
        printLog { "j#s#l#t#z#m#p#e#r#a [pass:$pass]" }
        if (!pass) scriptInError = true
        else n = null
    }
}